package com.pony.controller.admin;

import com.pony.dto.GoodsDTO;
import com.pony.dto.PageDTO;
import com.pony.dto.TypeDTO;
import com.pony.enums.AppHttpCodeEnum;
import com.pony.result.R;
import com.pony.service.IGoodsService;
import com.pony.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pony
 * @since 2023-11-16
 */
@Slf4j
@RestController
@RequestMapping("/admin/goods")
@CrossOrigin(origins = "*")
public class GoodsController {
    @Autowired
    private IGoodsService goodsService;

    /**
     * 上传酒水图片
     * @param file 图片
     * @return
     */
    @PostMapping("/uploadImg")
    public R uploadImg(MultipartFile file) {
        // 1、文件校验
        // 是否是图片文件 image/jpeg，是不是以 image 开头的
        if (!file.getContentType().startsWith("image")){
            return R.error(AppHttpCodeEnum.IMAGE_ERROR_TYPE);
        }
        // 判断文件大小不能超过 30MB
        if (file.getSize() > 30*1024*1024){
            return R.error(AppHttpCodeEnum.FILE_EXCEED);
        }
        log.info(file.toString());
        return goodsService.uploadImg(file);
    }

    /**
     * 添加酒水
     * @return
     */
    @PostMapping
    public R add(@RequestBody GoodsDTO goodsDto) {
        log.info("添加的酒水："+goodsDto.toString());
        // 参数校验
        if (StringUtils.isBlank(goodsDto.getName())){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        if (goodsDto.getOrderId()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        if (goodsDto.getTypeId()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return goodsService.add(goodsDto);
    }

    /**
     * 获取酒水分页信息
     * @param pageDTO
     * @return
     */
    @PostMapping("/page")
    public R getPage(@RequestBody PageDTO pageDTO){
        log.info("分页信息"+pageDTO.toString());
        return goodsService.getPage(pageDTO);
    }

    /**
     * 更改酒水状态
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/status/{id}/{status}")
    public void setStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status){
        goodsService.setStatus(id, status);
    }

    /**
     * 根据分类id，查询单个分类信息
     * @param id
     * @return
     */
    @GetMapping("/selectOne/{id}")
    public R selectOneById(@PathVariable("id") Integer id){
        return goodsService.selectOneById(id);
    }

    /**
     * 修改数据
     * @param goodsDTO
     * @return
     */
    @PutMapping
    public R updateSinger(@RequestBody GoodsDTO goodsDTO){
        // 参数校验
        if (StringUtils.isBlank(goodsDTO.getName())){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return goodsService.updateSinger(goodsDTO);
    }

    /**
     * 根据id删除酒水
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id") Integer id) {
        return goodsService.deleteOneById(id);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("deleteBatch")
    public R deleteBatch(@RequestBody List<Integer> ids){
        log.info(ids.toString());
        if(ids == null || ids.size() ==0 ){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return goodsService.deleteBatch(ids);
    }

    /**
     * 修改数据
     * @param goodsDTO
     * @return
     */
    @PutMapping("/updateType")
    public R updateGoods(@RequestBody GoodsDTO goodsDTO){
        return goodsService.updateType(goodsDTO);
    }
}
